
#import "BandwidthViewController.h"

@implementation BandwidthViewController

//
// bandwidth
//
@synthesize bandwidth														= _bandwidth;

//
// init
//
- (id)initWithBandwidth:(NSString*)bandwidth
{
	//
	// call super
	//
	self																	= [super initWithStyle:UITableViewStyleGrouped];
	if(!self)
		return nil;
	
	//
	// setup title,image
	//
	self.title																= NSLocalizedStringFromTable(@"Bandwidth", @"BandwidthViewController", nil);
	
	//
	// setup bandwidth
	//
	if(bandwidth.length)
	{
		NSError* error														= nil;
		NSRegularExpression* regex											= [NSRegularExpression regularExpressionWithPattern:@"^\\s*([^\\s]+)\\s*([^\\s]+)\\s*([^\\s]+)\\s*$" options:NSRegularExpressionAnchorsMatchLines error:&error];
		NSTextCheckingResult* match											= error || !regex ? nil : [regex firstMatchInString:bandwidth options:0 range:NSMakeRange(0, bandwidth.length)];
		if(match && match.numberOfRanges == 4)
		{
			self.bandwidth													= [NSMutableArray arrayWithCapacity:3];
			[self.bandwidth addObject:[bandwidth substringWithRange:[match rangeAtIndex:1]]];
			[self.bandwidth addObject:[bandwidth substringWithRange:[match rangeAtIndex:2]]];
			[self.bandwidth addObject:[bandwidth substringWithRange:[match rangeAtIndex:3]]];
		}
	}
	
	return self;
}

//
// dealloc
//
- (void)dealloc
{
	self.bandwidth															= nil;
	
	[super dealloc];
}

//
// get section count
//
- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView
{
	return 1;
}

//
// get row count
//
- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
	return self.bandwidth.count;
}

//
// get footer
//
- (NSString*)tableView:(UITableView*)tableView titleForFooterInSection:(NSInteger)section
{
	return self.bandwidth ? nil : NSLocalizedStringFromTable(@"He(She) does not set bandwidth info", @"BandwidthViewController", nil);
}

//
// get cell
//
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
	UITableViewCell* theCell												= [tableView dequeueReusableCellWithIdentifier:@"v1"];
	if(!theCell)
		theCell																= [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"v1"] autorelease];
	
	if(indexPath.row == 0)
		theCell.textLabel.text												= NSLocalizedStringFromTable(@"Download", @"BandwidthViewController", nil);
	else if(indexPath.row == 1)
		theCell.textLabel.text												= NSLocalizedStringFromTable(@"Upload", @"BandwidthViewController", nil);
	else
		theCell.textLabel.text												= NSLocalizedStringFromTable(@"ISP", @"BandwidthViewController", nil);
	
	theCell.detailTextLabel.text											= [self.bandwidth objectAtIndex:indexPath.row];
	theCell.selectionStyle													= UITableViewCellSelectionStyleNone;
	return theCell;
}

@end
